package com.kapo.egroup.controller;

import com.kapo.egroup.common.ResponseEnum;
import com.kapo.egroup.common.Result;
import com.kapo.egroup.entity.base.project.Project;
import com.kapo.egroup.entity.base.recruit.Recruit;
import com.kapo.egroup.entity.base.talent.Talent;
import com.kapo.egroup.security.domain.LoginUser;
import com.kapo.egroup.service.RecruitService;
import com.kapo.egroup.service.TalentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title CommunityController
 * @Description TODO
 * @Author 86159
 * @Date 2023-04-01 15:19
 **/
@RestController
@RestControllerAdvice
@RequestMapping("/api/community")
public class CommunityController {
    @Autowired
    private TalentService talentService;

    @Autowired
    private RecruitService recruitService;

    /**
     * @descriptions 获取所有招募信息
     * @author 黄家宝
     * @date 2022/8/18 17:28
     * @return com.eintern.common.Result
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/recruit")
    public Result getAllRecruit() throws Exception{
        List<Recruit> recruitList = recruitService.getRecruitAll();
        System.out.println(recruitList);
        return Result.ok(recruitList);
    }

    /**
     * @descriptions 创建招募信息
     * @author 黄家宝
     * @date 2022/8/18 17:28
     * @return com.eintern.common.Result
     */
    @CrossOrigin
    @ResponseBody
    @PostMapping("/recruit")
    public Result createRecruit(@RequestBody Recruit recruit) throws Exception{
        Long loginUserId = getCurrentUserId();
        if(!recruit.getLeaderId().equals(loginUserId)){
            return Result.fail().result(ResponseEnum.EXCEPTION);
        }
        recruit.setState(0);
        recruitService.save(recruit);
        return Result.ok();
    }

    /**
     * @descriptions 删除招募信息
     * @author 黄家宝
     * @date 2022/8/18 17:28
     * @return com.eintern.common.Result
     */
    @CrossOrigin
    @ResponseBody
    @DeleteMapping("/recruit")
    public Result deleteRecruit(@RequestBody Recruit recruit) throws Exception{
        Long loginUserId = getCurrentUserId();
        if(!recruit.getLeaderId().equals(loginUserId)){
            return Result.fail().result(ResponseEnum.EXCEPTION);
        }
        recruitService.removeById(recruit.getId());
        return Result.ok();
    }

    /**
     * @descriptions 获取所有人才信息
     * @author 黄家宝
     * @date 2022/8/18 17:28
     * @return com.eintern.common.Result
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/talent")
    public Result getAllTalent() throws Exception{
        List<Talent> talentList = talentService.getTalentAll();
        System.out.println(talentList);
        return Result.ok(talentList);
    }

    /**
     * @descriptions 创建人才信息
     * @author 黄家宝
     * @date 2022/8/18 17:28
     * @return com.eintern.common.Result
     */
    @CrossOrigin
    @ResponseBody
    @PostMapping("/talent")
    public Result createTalent(@RequestBody Talent talent) throws Exception{
        Long loginUserId = getCurrentUserId();
        if(!talent.getUserId().equals(loginUserId)){
            return Result.fail().result(ResponseEnum.EXCEPTION);
        }
        talent.setState(0);
        talentService.save(talent);
        return Result.ok();
    }

    /**
     * @descriptions 删除人才信息
     * @author 黄家宝
     * @date 2022/8/18 17:28
     * @return com.eintern.common.Result
     */
    @CrossOrigin
    @ResponseBody
    @DeleteMapping("/talent")
    public Result deleteTalent(@RequestBody Talent talent) throws Exception{
        Long loginUserId = getCurrentUserId();
        if(!talent.getUserId().equals(loginUserId)){
            return Result.fail().result(ResponseEnum.EXCEPTION);
        }
        talentService.removeById(talent.getId());
        return Result.ok();
    }

    private Long getCurrentUserId(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object user = authentication.getPrincipal();
        LoginUser loginUser = (LoginUser)user;
        return loginUser.getUserId();
    }
}
